#include "audioinfooutput.h"

AudioInfoOutPut::AudioInfoOutPut(OpenSl_Engine *pOpenSlEngine):m_pOpenSlEngine(pOpenSlEngine)
{
    SLresult result;
    // configure audio source
    SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
    SLDataFormat_PCM format_pcm = {SL_DATAFORMAT_PCM, 1, SL_SAMPLINGRATE_8,
        SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16,
        SL_SPEAKER_FRONT_CENTER, SL_BYTEORDER_LITTLEENDIAN};
    SLDataSource audioSrc = {&loc_bufq, &format_pcm};

    // configure audio sink
    SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, m_pOpenSlEngine->outputMixObject};
    SLDataSink audioSnk = {&loc_outmix, NULL};

    // create audio player
    const SLInterfaceID ids[3] = {SL_IID_BUFFERQUEUE, SL_IID_EFFECTSEND,
            /*SL_IID_MUTESOLO,*/ SL_IID_VOLUME};
    const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE,
            /*SL_BOOLEAN_TRUE,*/ SL_BOOLEAN_TRUE};

    result = (*(pOpenSlEngine->engineEngine))->CreateAudioPlayer(pOpenSlEngine->engineEngine, &bqPlayerObject, &audioSrc, &audioSnk,
            3, ids, req);

    result = (*bqPlayerObject)->Realize(bqPlayerObject, SL_BOOLEAN_FALSE);

    // get the play interface
    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_PLAY, &bqPlayerPlay);

    // get the buffer queue interface
    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_BUFFERQUEUE,
            &bqPlayerBufferQueue);

    // register callback on the buffer queue
    result = (*bqPlayerBufferQueue)->RegisterCallback(bqPlayerBufferQueue, bqPlayerCallback, this);


    // get the effect send interface
    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_EFFECTSEND,
            &bqPlayerEffectSend);

    // get the volume interface
    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_VOLUME, &bqPlayerVolume);

     qDebug(QString("---------------").toLocal8Bit().data());

}

AudioInfoOutPut::~AudioInfoOutPut()
{
    SLresult result;
    result = (*bqPlayerBufferQueue)->Clear(bqPlayerBufferQueue);

    (*bqPlayerObject)->Destroy(bqPlayerObject);
     bqPlayerObject = 0;
}

void AudioInfoOutPut::start()
{
    SLresult result;

    // set the player's state to playing
    result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_PLAYING);
    if(SL_RESULT_SUCCESS != result)
        qDebug(QString("PlayerCallback7777777").toLocal8Bit().data());

    qDebug(QString("=====================").toLocal8Bit().data());

    bE = true;
}

void AudioInfoOutPut::stop()
{
    SLresult result;
    result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay, SL_PLAYSTATE_STOPPED);
}


void AudioInfoOutPut::bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
{
    AudioInfoOutPut *pContext = (AudioInfoOutPut *)context;
    if(pContext)
        pContext->PlayerCallback(bq);
}

void AudioInfoOutPut::PlayerCallback(SLAndroidSimpleBufferQueueItf bq)
{
//    CCriticalAutoLock loAutoLock(oCriSection);
//    if(!msg_queue_audio.empty())
//    {
//        m_OutPut_buf = msg_queue_audio.front();
//        msg_queue_audio.pop();
//        SLresult result;
//        result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, m_OutPut_buf.data(),m_OutPut_buf.length());
//    }else
//    {
//        qDebug(QString(" not Auto data ........").toLocal8Bit().data());
//    }

//    qDebug(QString("PlayerCallbackxxxxxxxxxx").toLocal8Bit().data());

//    if(bq)
//    {
//        CCriticalAutoLock loAutoLock(oCriSection);
//        if(m_OutPut_buf.empty())
//        {
//            bE = true;
//        }else
//        {
//            SLresult result;
//            result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, m_OutPut_buf.data(),m_OutPut_buf.length());
//            m_OutPut_buf.clear();
//        }
//    }else
//    {
//        CCriticalAutoLock loAutoLock(oCriSection);
//        if(bE)
//        {
//            SLresult result;
//            result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, m_OutPut_buf.data(),m_OutPut_buf.length());
//            m_OutPut_buf.clear();
//            bE = false;
//        }
//    }


    ////////////////////////////
        CCriticalAutoLock loAutoLock(oCriSection);

        if(!m_OutPut_buf.empty())
        {
            SLresult result;
            result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, m_OutPut_buf.data(),m_OutPut_buf.length());
            m_OutPut_buf.clear();
        }else
        {
            //qDebug(QString("Auto data 22222222........").toLocal8Bit().data());
            bE = true;
        }
}


//qint64 AudioInfoOutPut::readData(char *data, qint64 maxlen)
//{
//    CCriticalAutoLock loAutoLock(oCriSection);

//    if(m_OutPut_buf.size() >= maxlen)
//    {
//        std::string str_buf =m_OutPut_buf.substr(0,maxlen);

//        memcpy(data,str_buf.data(),maxlen);

//        m_OutPut_buf = m_OutPut_buf.erase(0,maxlen);
//    }else
//    {
//        memset(data,0,maxlen);
//        //m_OutPut_buf.clear();
//       // return m_OutPut_buf.size();
//    }
//    return maxlen;
//}

bool AudioInfoOutPut::receive_handler(char *data, int size)
{
//    int  bl= 1;
//    {
//        CCriticalAutoLock loAutoLock(oCriSection);
//        bl = msg_queue_audio.size();
//        msg_queue_audio.push(std::string(data,size));
//    }
//    if(bl == 0)
//    {
//        PlayerCallback(NULL);
//    }
    /////////////////////////////
//    SLresult result;
//    result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, data,size);
//    usleep(10000);

    /////////////////////////////
    CCriticalAutoLock loAutoLock(oCriSection);
    if(bE)
    {
        SLresult result;
        result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, data,size);
        bE = false;
    }else
    {
        m_OutPut_buf.append(std::string(data,size));//添加进去
    }
}
